<template>
	<view class="container">
		
		<view class="form">
			<view class="selectpic">
				<uni-file-picker :image-styles="imageStyles" :list-styles="listStyles" file-mediatype="image" mode="grid" file-extname="png,jpg" 
				:limit="1" ref="files" :auto-upload="false" @select="select"/>
			</view>
			<view> <text>商品名称:</text><input  type="text" v-model="obj.spmc" /> </view>
			<!-- <view> <text>商品信息编号:</text><input  type="text" v-model="obj.spxxbh" /> </view> -->
			<view> <text>商品单价:</text><input type="text" v-model="obj.spdj"/> </view>
			<!-- <view> <text>商品折扣:</text><input type="text" v-model="obj.spzk"/> </view> -->
			<!-- s -->
			<view> <uni-combox label="商品分类" :candidates="candidates" @input="inputbase"
			 placeholder="请选择商品分类(必选)" v-model="base"></uni-combox>
			</view>
			<view> <uni-combox label="活动分类" :candidates="appendcandidates" @input="inputappend"
			  placeholder="请选择活动分类(非必选)" v-model="append"></uni-combox> </view>
			<view class="special">
				<button type="primary" size="mini" @click="confirm">确认添加</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {addgoods_1} from '@/api/addgoods/addgoods.js'
	import {addgoods_2} from '@/api/addgoods/addgoods.js'
	import {BASE_URL} from '../../api/ajax.js'
	import {updatecandidate} from '@/api/addgoods/addgoods.js'//请求基础类型数据并更新combox待选项，最多8种，否则会报错
	import {updateappendcandidate} from '@/api/addgoods/addgoods.js'
	export default {
		data(){
			 return{
				obj:{
					goodsID: "",
					shopID: "1001",
					spmc: "",
					spdj: "",
					mybasetypeId:"",
					myappendtypeId:"",
					sptpcflj: "",
					spzk:"",
					spjs:"",
					spxq:"",
					sppf:"",	
				},
				candidates:[],
				appendcandidates:[],//活动分类候选项，由onload函数实时更新
				baseitems: [],
				appenditems: [],
				file:"",
				base:'',
				append:'',
				filepath:'',
				imageStyles:{
				                width:100,
				                height:100,
				                border:{
				                    color:"#000000",
				                    width:2,
				                    style:'dashed',
				                    radius:'2px'
				                }
				            },
				 listStyles:{
				                // 是否显示边框
				                border: true,
				                // 是否显示分隔线
				                dividline: true,
				                // 线条样式
				                borderStyle: {
				                    width:1,
				                    color:"#000000",
				                    radius:2
				                }
				            }
				
			};
		},

		
		methods:{
			select(res){
				this.filepath=res.tempFilePaths[0]
				console.log(this.filepath)
						},
			inputbase(ref){
	/* 			console.log(this.baseitems.length)
				console.log(this.baseitems) */
				console.log("base组合框输入")
				let index = this.candidates.indexOf(ref)
				console.log(index)
				if (index+1)//index=-1表示输入框内容不在候选项里面，若执行下列代码会导致控制台报错
				{console.log(this.baseitems[index])
				this.obj.mybasetypeId=this.baseitems[index].basetypeId
				console.log(this.obj.mybasetypeId)}
			},
			inputappend(ref){
				console.log("append组合框输入")
				let index = this.appendcandidates.indexOf(ref)
				console.log(index)
				if (index+1)//index=-1表示输入框内容不在候选项里面，若执行下列代码会导致控制台报错
				{
					console.log(this.appenditems[index])
				this.obj.myappendtypeId=this.appenditems[index].appendtypeId
				console.log(this.obj.myappendtypeId)
				}
			},
			/* 请求基础类型数据信息并更改 */
			upbasetypedata(){
				console.log("基础类型更新函数被调用")
				uni.request({
						// url:options.url,
						url:BASE_URL+'/supermarket/goods/getBaseType',
						method: 'GET',
						data: {},
						success: (res)=>{
							console.log(res.data.data.items)
							this.baseitems = res.data.data.items
							/* console.log(this.baseitems) */
							this.candidates = updatecandidate(this.baseitems)
							/* if (this.candidates)
								console.log(this.baseitems) */
							uni.hideLoading()
							
						},
						fail: (err)=>{
							uni.showToast({
								title: '请求接口失败'
							})
							reject(err)
						}
					})
				/* console.log("我是基础类型更新函数") */
				},
			upappendtypedata(){
				console.log("我是活动类型更新函数")
				uni.request({
						// url:options.url,
						url:BASE_URL+'/supermarket/goods/getAppendType',
						method: 'GET',
						data: {},
						success: (res)=>{
							console.log(res.data.data.items)
							this.appenditems = res.data.data.items
							/* console.log(this.baseitems) */
							this.appendcandidates = updateappendcandidate(this.appenditems)
							/* if (this.candidates)
								console.log(this.baseitems) */
							console.log(this.appendcandidates)
							uni.hideLoading()
							
						},
						fail: (err)=>{
							uni.showToast({
								title: '请求接口失败'
							})
							reject(err)
						}})
			},
			confirm(){
				if (!this.filepath||!this.obj.spmc||!this.obj.spdj||!this.obj.mybasetypeId)
				{
					uni.showToast({
						title:'请完善商品信息'
					})
					
				}
				else
				{console.log('检查数据')
				console.log(this.filepath)
				console.log(this.obj)
				console.log('开始发送')
				/* console.log(typeof this.obj.mybasetypeId) */
				/* addgoods_1(this.filepath,this.obj.spmc,this.obj.spdj,this.obj.mybasetypeId,this.obj.shopID) */
				if(this.obj.myappendtypeId)//是否有活动分类
				{	
					addgoods_2(this.filepath,this.obj.spmc,this.obj.spdj,this.obj.mybasetypeId,this.obj.myappendtypeId,this.obj.shopID)
					
					/* console.log(this.filepath)
					console.log(this.obj.spmc)
					console.log(this.obj.spdj)
					console.log(this.obj.mybasetypeId)
					console.log(this.obj.shopID)
					console.log()
					console.log()
					addgoods_1(this.filepath,this.obj.spmc,this.obj.spdj,this.obj.mybasetypeId,this.obj.shopID)	 */
				}
				else
				{
					/* console.log(this.filepath)
					console.log(this.obj.spmc)
					console.log(this.obj.spdj)
					console.log(this.obj.mybasetypeId)
					console.log(this.obj.shopID) */
					addgoods_1(this.filepath,this.obj.spmc,this.obj.spdj,this.obj.mybasetypeId,this.obj.shopID)	
/* 					addgoods_2(this.filepath,this.obj.spmc,this.obj.spdj,this.obj.mybasetypeId,this.obj.myappendtypeId,this.obj.shopID)
 */					}
				/* console.log('1') */
				this.obj.spmc=''
				this.obj.spdj=''
				this.filepath=''

				}
			
				
			},
			
		},
		
		computed:{
			
		},
		onLoad() {
		/* 	console.log("a") */
			this.upbasetypedata()//请求基本类型信息并更改
			this.upappendtypedata()//请求附加类型信息并更改
		/* 	console.log(this.baseitems) */
		},
		}
</script>
<style lang="scss" scoped>

	.container{
		position:relative ;
	}
	.notice{
		padding: 20rpx;
	}
	.selectpic{
		float:right;
	}
	.form{
		// border: 1rpx solid #f00;
		padding: 10rpx;
		// display: flex;
		// flex-direction: column;
		// align-items: center;
		view{
			// border: 1rpx solid #00f;
			margin-bottom: 50rpx;
			
		}
		text{
			font-weight: bold;
			font-size: 33rpx;
			
		}
		input{
			border-bottom: 1rpx solid #b2b2b2;
			// border: 1rpx solid #f00;
			display: inline-block;
			vertical-align: middle;
			// text-align: cener;
		}
		image{
			background-color:#EEEEEE;
			height:120px;
			width:120px;
			float:center;
		}
		.special{
			text-align: center;
		}
	}
</style>
